/**
 * @author 雷明浩
 * @date 2025/5/30 14:23
 */
public class IsValidBST {
    // leetcode 验证二叉搜索树
    // https://leetcode.cn/problems/validate-binary-search-tree/?envType=study-plan-v2&envId=top-100-liked
    private long prev = (long)Integer.MIN_VALUE-1;
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        boolean leftResult = isValidBST(root.left);
        if (!leftResult) {
            return false;
        }
        if (root.val <= prev) {
            return false;
        }
        prev = root.val;
        return isValidBST(root.right);
    }
}